﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using ActiveUp.Net.Mail;
using Metro.Core;

namespace Mail
{
    /// <summary>
    /// Interaction logic for WidgetControl.xaml
    /// </summary>
    public partial class WidgetControl : UserControl
    {
        private Imap4Client imap;
        private DispatcherTimer timer;
        private int unreadCount;
        private Options _options;

        public WidgetControl()
        {
            InitializeComponent();
        }

        public void Load()
        {
            if (!string.IsNullOrEmpty(Widget.Instance.Sett.Username) && !string.IsNullOrEmpty(Widget.Instance.Sett.Username))
            {
                imap = new Imap4Client();
                imap.BeginConnectSsl("imap.gmail.com", 993, null);
                imap.Connected += new ConnectedEventHandler(imap_Connected);
                timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(30) };
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }

            var optionsItem = new MenuItem { Header = Widget.Instance.LocaleManager.GetString("Options") };
            optionsItem.Click += OptionsItemClick;
            Widget.Instance.AddMenuItem(optionsItem);

            _options = new Options();
        }

        void OptionsItemClick(object sender, RoutedEventArgs e)
        {
            if (_options.IsVisible)
            {
                _options.Activate();
                return;
            }
            _options = new Options();

            if (E.Locale == "he-IL" || E.Locale == "ar-SA")
            {
                _options.FlowDirection = FlowDirection.RightToLeft;
            }
            else
            {
                _options.FlowDirection = FlowDirection.LeftToRight;
            }

            _options.ShowDialog();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            GetUnreadCount(null);
        }

        void imap_Connected(object sender, ConnectedEventArgs e)
        {
            imap.BeginLoginFast(Widget.Instance.Sett.Username, Widget.Instance.Sett.Password, GetUnreadCount);
        }

        private void GetUnreadCount(IAsyncResult ar)
        {
            if (imap.IsConnected)
            {
                var count = imap.SelectMailbox("INBOX").Search("UNSEEN").Length;
                if (unreadCount == 0 && count > 0)
                {
                    UnreadCount.Dispatcher.Invoke((Action)delegate
                                                               {
                                                                   var s =
                                                                       (Storyboard)Resources["ShowCountAnim"];
                                                                   s.Begin();
                                                               }, null);
                }
                else if (unreadCount > 0 && count == 0)
                {
                    UnreadCount.Dispatcher.Invoke((Action)delegate
                                                               {
                                                                   var s =
                                                                       (Storyboard)Resources["HideCountAnim"];
                                                                   s.Begin();
                                                               }, null);
                }
                unreadCount = count;
                UnreadCount.Dispatcher.Invoke((Action)delegate
                                                           {
                                                               UnreadCount.Text = unreadCount.ToString();
                                                           }, null);
            }
        }

        public void UpdateSettings()
        {
            if (imap != null && imap.IsConnected)
            {
                if (!string.IsNullOrEmpty(Widget.Instance.Sett.Username) && !string.IsNullOrEmpty(Widget.Instance.Sett.Password))
                    imap.LoginFast(Widget.Instance.Sett.Username, Widget.Instance.Sett.Password);
            }
            else
            {
                imap = new Imap4Client();
                imap.BeginConnectSsl("imap.gmail.com", 993, null);
                imap.Connected += new ConnectedEventHandler(imap_Connected);
                timer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(30) };
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }
        }
    }
}
